package view.CURD;

import library.controller.FrameListener;
import library.po.BookType;
import library.service.impl.BookTypeServiceImpl;
import library.service.BookTypeService;
import view.main.MainFrame;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author unknown
 */
public class BookTypeDialog extends JDialog {
    public BookTypeDialog(MainFrame owner) {
        super(owner);
        this.setTitle("添加图书类别");
        this.listener = owner.getMainListener();
        this.setVisible(true);
        initComponents();
    }

    private void initComponents() {
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        typeName = new JLabel();
        typeValue = new JTextField();
        okButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(
                    new javax.swing.border.EmptyBorder(0, 0, 0, 0), ""
                    , javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.BOTTOM
                    , new java.awt.Font("D\u0069al\u006fg", java.awt.Font.BOLD, 12)
                    , java.awt.Color.red), dialogPane.getBorder()));
            dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                @Override
                public void propertyChange(java.beans.PropertyChangeEvent e
                ) {
                    if ("\u0062or\u0064er".equals(e.getPropertyName())) throw new RuntimeException()
                            ;
                }
            });
            dialogPane.setLayout(null);

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- typeName ----
                typeName.setText("类型名：");
                typeName.setFont(new Font("\u5e7c\u5706", Font.PLAIN, 16));
                contentPanel.add(typeName);
                typeName.setBounds(new Rectangle(new Point(25, 40), typeName.getPreferredSize()));
                //---- typeValue
                contentPanel.add(typeValue);
                typeValue.setBounds(95, 40, 135, typeValue.getPreferredSize().height);
                //---- okButton ----
                okButton.setText("确认");
                okButton.addActionListener(listener);
                okButton.setActionCommand("AddBookTypePost");
                contentPanel.add(okButton);
                okButton.setBounds(80, 70, 110, 35);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for (int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel);
            contentPanel.setBounds(10, 15, 278, 200);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < dialogPane.getComponentCount(); i++) {
                    Rectangle bounds = dialogPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialogPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialogPane.setMinimumSize(preferredSize);
                dialogPane.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(305, 200);
        setLocationRelativeTo(getOwner());
    }
    public BookType getBookType(){
        BookType bookType = new BookType();
        bookType.setBookTypeName(this.typeValue.getText().trim());
        return bookType;
    }
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel typeName;
    private JTextField typeValue;
    private JButton okButton;
    private FrameListener listener;
    private BookTypeService bookTypeService = new BookTypeServiceImpl();
}
